package com.easyj.admin.sys.serviceimpl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.easyj.admin.sys.service.DirtionaryService;
import com.easyj.base.exception.EasyjException;
import com.easyj.base.exception.ValidateException;
import com.easyj.base.security.service.SecurityUtil;
import com.easyj.admin.db.mapper.DirtionaryItemMapper;
import com.easyj.admin.db.mapper.DirtionaryMapper;
import com.easyj.admin.db.model.Dirtionary;
import com.easyj.admin.db.model.DirtionaryItem;





@Service
public class DirtionaryServiceImpl implements DirtionaryService{
	
	private final String DirCahceName="dirtionarys";
	private final String ItemCahceName="dirtionaryItems";

	@Autowired
	private  DirtionaryMapper dirtionaryMapper;
	
	@Autowired
	private  DirtionaryItemMapper dirtionaryItemMapper;

	
	public List<Dirtionary> getAllDirtionary(){
		
		Dirtionary filter =new Dirtionary();
		//filter.setStatus(1);//
		List<Dirtionary> roots= dirtionaryMapper.selectByExample(filter);
		
		DirtionaryItem  itemQO=new DirtionaryItem();
		List<DirtionaryItem> items=dirtionaryItemMapper.selectByExample(itemQO);
		
		for(Dirtionary r:roots){
			r.setItems(new ArrayList<DirtionaryItem>());
		}
		for(DirtionaryItem item:items){
			for(Dirtionary r:roots){
			    if(r.getCode().equals(item.getParentCode())){
			    	r.getItems().add(item);
			    }
			}
		}
		
		return roots;
	}
	
	


	public Dirtionary findById(Long id){
		Dirtionary entity= dirtionaryMapper.selectByPrimaryKey(id);
		return entity;
	}
	
	
	@Transactional
	public int  insertEntity(Dirtionary entity) throws EasyjException{
		if(dirtionaryMapper.selectUniqueByCode(entity.getCode())!=null) {
			throw new ValidateException(EasyjException.SameCode,"已有相同编号的字典");
		}
		entity.setOperatorId(SecurityUtil.getOperatorId());
		entity.setCreateTime(new Date());
		entity.setModifyTime(new Date());
		int rtn= dirtionaryMapper.insert(entity);
		return rtn;
	}

	@Transactional
	public int  updateEntity(Dirtionary entity){
		Dirtionary find=dirtionaryMapper.selectByPrimaryKey(entity.getId());
		
		find.setName(entity.getName()); //only update name
		find.setOperatorId(SecurityUtil.getOperatorId());
		find.setModifyTime(new Date());
		
		int rtn= dirtionaryMapper.updateByPrimaryKey(find);
		return rtn;
	}


	
//================字典条目===================================================
	@Transactional
	@CacheEvict(value=DirCahceName, key="#item.parentCode")
	public int addItem(DirtionaryItem item) throws EasyjException{
		if(dirtionaryMapper.selectUniqueByCode(item.getParentCode())==null)
			throw new EasyjException(EasyjException.UnDefine,"数据错误！");
		DirtionaryItem find=dirtionaryItemMapper.selectUniqueByItem(item.getParentCode(), item.getCode());
		if(find!=null){
			throw new ValidateException(EasyjException.SameCode,"已有相同编号的条目");
		}
		item.setIsSystem(false);
		item.setOperatorId(SecurityUtil.getOperatorId());
		item.setModifyTime(new Date());
		item.setStatus(1);
		return dirtionaryItemMapper.insert(item);
	}
	
	
	
	@Transactional
	public int  updateItem( DirtionaryItem entity) throws EasyjException{
		 DirtionaryItem item=dirtionaryItemMapper.selectByPrimaryKey(entity.getId());
		 if(item.getIsSystem()) {
			 throw new EasyjException(EasyjException.UpdateSystemObject,"数据错误系统预设置，不可删除！！");
				
			}
		 item.setStatus(entity.getStatus());
		 item.setName(entity.getName()); //only update name
		 item.setOperatorId(SecurityUtil.getOperatorId());;
		 item.setModifyTime(new Date());
		 return dirtionaryItemMapper.updateByPrimaryKeySelective(item);
	}
	
	@CacheEvict(value=DirCahceName, key="#item.parentCode")
	public  void  evictDirtionary( DirtionaryItem item){
		 
	}
	
	@CacheEvict(value=ItemCahceName,key="#entity.parentCode+'$&'+#entity.code")
	public void  evictDirtionaryItem( DirtionaryItem entity){
		 
	}
	
	public DirtionaryItem getItemById(Long id){
		return dirtionaryItemMapper.selectByPrimaryKey(id);
	}
	
	
	@Cacheable(value=ItemCahceName,key="#parentCode+'$&'+#itemCode" )
	public String getDirtionaryItemName(String parentCode,String itemCode){
		DirtionaryItem example=new DirtionaryItem();
		example.setParentCode(parentCode);
		example.setCode(itemCode);
		DirtionaryItem find= dirtionaryItemMapper.selectUniqueByItem(parentCode, itemCode);
		if(find!=null ){
			return find.getName();
		}else {
			return "";
		}
	}
	
	/**
	 * @param dirCode
	 * @return
	 */
	@Cacheable(value=DirCahceName,key="#parentCode")
	public List<DirtionaryItem> getDirtionaryItems(String parentCode){
		DirtionaryItem example=new DirtionaryItem();
		example.setParentCode(parentCode);
		example.setStatus(1);//
		List<DirtionaryItem> find= dirtionaryItemMapper.selectByExample(example);
		return find;
	}



	
}

